<template>
  <div class="xmind-list-shares">
    <div class="xminds-card-grid">
      <el-skeleton :loading="skeletonLoading" animated>
        <template #template>
          <div class="card-skeleton-title"><el-skeleton-item /></div>
          <div class="card-skeleton"><el-skeleton-item v-for="item in 10" :key="item" /></div>
        </template>
        <template #default>
          <div v-show="shareType === 1">
            <div class="box-modal" v-if="mindList.length">
              <div class="title">分享导图</div>
              <div class="list">
                <div class="mind" v-for="item in mindList" :key="item.mindId" @click="handleMindClick(item.mindId)">
                  <div class="image-preview">
                    <img :src="item.coverImage" alt="cover">
                  </div>
                  <div class="desc">
                    <div class="name">
                      <span class="mind-map">{{ item.mindName }}</span>
                    </div>
                    <p class="source-share">
                      <img :src="item.sourceAvatar" alt="" class="source-avatar">
                      <span class="nickname">{{ item.sourceUserName }}</span>
                      <span class="share-time">{{ item.shareTime }}</span>
                    </p>
                  </div>
                </div>
              </div>
            </div>
            <div class="empty-mind-text" v-if="!mindList.length">未找到Gmind文件</div>
          </div>
          <div v-show="shareType === 2">
            <div class="box-modal" v-if="shareOtherMindList.length">
              <div class="title">我的分享</div>
              <div class="list">
                <div class="mind" v-for="item in shareOtherMindList" :key="item.id" @click="handleMindClick(item.id)">
                  <div class="image-preview">
                    <img :src="item.coverImage" alt="cover">
                  </div>
                  <div class="desc desc-o">
                    <div class="name">
                      <span class="mind-map">{{ item.name }}</span>
                      <div class="share-users" @click.stop="handleShowShareUserModal(item.id, item.shareUsers)">
                        <i class="ri-group-line"></i>
                        分享成员：
                        <img src="@/assets/images/home/user-share.png" alt="">
                        <span class="count">x{{ item.shareUsers.length }}</span>
                      </div>
                    </div>
                    <div class="tip">最后修改：{{ item.updateTime }}</div>
                  </div>
                </div>
              </div>
            </div>
            <div class="empty-mind-text" v-if="!shareOtherMindList.length">未找到Gmind文件</div>
          </div>
        </template>
      </el-skeleton>
    </div>
    <ShareUserList
      v-if="shareUserListVisible"
      :userList="shareUserList"
      :mindId="currentMindId"
      @close-modal="handleCloseShareUserModal" />
  </div>
</template>

<script setup lang="ts">
import { ref, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import mitter from '@/mitt'
import { apiGetMindShareList, apiGetMindShareToOtherList } from '@/apis/work'
import type { ShareMind, ShareToOtherMind, User } from '@/types'
import ShareUserList from './components/ShareUserList.vue'

const router = useRouter()
const searchKeywords = ref('')
const mindList = ref<ShareMind[]>([])
const shareOtherMindList = ref<ShareToOtherMind[]>([])
const shareType = ref(1)
const skeletonLoading = ref(false)
const shareUserListVisible = ref(false)

/**
 * 与我分享的导图列表数据查询
 */
const queryMindSharePageList = async () => {
  skeletonLoading.value = true
  try {
    const data = await apiGetMindShareList<ShareMind[]>({
      name: searchKeywords.value
    })
    mindList.value = data
  } finally {
    skeletonLoading.value = false
  }
}

/**
 * 分享给别人的导图列表
 */
const queryShareToOtherPageList = async () => {
  skeletonLoading.value = true
  try {
    const data = await apiGetMindShareToOtherList<ShareToOtherMind[]>({
      name: searchKeywords.value
    })
    shareOtherMindList.value = data
  } finally {
    skeletonLoading.value = false
  }
}

const searchMindPageList = () => {
  if (shareType.value === 1) {
    queryMindSharePageList()
  } else {
    queryShareToOtherPageList()
  }
}
searchMindPageList()

const shareUserList = ref<User[]>([])
const currentMindId = ref('')
const handleShowShareUserModal = (mindId: string, users: User[]) => {
  currentMindId.value = mindId
  shareUserList.value = users
  shareUserListVisible.value = true
}

const handleCloseShareUserModal = (refresh) => {
  shareUserListVisible.value = false
  if (refresh) {
    searchMindPageList()
  }
}

const handleMindClick = (mindId: string) => {
  router.push(`/mind-map/${mindId}`)
}

mitter.on('search-mind-list', function (keywords?: any) {
  searchKeywords.value = keywords
  searchMindPageList()
})

mitter.on('share-type-change', function (type: any) {
  shareType.value = type
  searchMindPageList()
})

onBeforeUnmount(() => {
  mitter.off('search-mind-list')
  mitter.off('share-type-change')
})
</script>

<style lang="scss" scoped>
.xminds-card-grid {
  padding: 8px 2px;
  min-height: 360px;

  .card-skeleton-title {
    margin-top: 36px;

    .el-skeleton__item {
      width: 160px;
      height: 36px;
    }
  }

  .card-skeleton {
    margin-top: 20px;
    gap: 16px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);

    .el-skeleton__item {
      height: auto;
      height: 150px;
    }
  }

  .box-modal {
    &+.box-modal {
      margin-top: 36px;
    }

    .title {
      font-size: 13px;
      line-height: 18px;
      color: #949699;
      display: flex;
      margin-top: 6px;
      height: 36px;
      align-items: center;
      justify-content: space-between;
    }

    .list {
      display: grid;
      grid-gap: 24px 32px;
      grid-template-columns: repeat(auto-fill, minmax(275px, 1fr));
      margin-top: 12px;

      .mind {
        border: 1px solid rgba(0, 0, 0, .08);
        border-radius: 8px;
        padding: 0;
        background-color: #FAFAFB;
        position: relative;

        .logo {
          font-size: 0;

          img {
            width: 32px;
          }
        }

        .image-preview {
          font-size: 0;
          border-bottom: 1px solid rgba(0, 0, 0, .08);
          aspect-ratio: 2.05;
          overflow: hidden;
          border-radius: 8px 8px 0 0;

          &:hover {
            img {
              transform: scale(1.3);
            }
          }

          img {
            width: 100%;
            height: 100%;
            -o-object-fit: cover;
            object-fit: cover;
            max-width: 100%;
            display: block;
            transition: 0.3s;
          }
        }

        .desc {
          margin-top: 8px;
          padding: 8px 14px 12px 14px;
          position: relative;

          .name {
            font-size: 12px;
            color: #202020;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .mind-map {
              display: block;
              font-weight: 600;
              max-width: 50%;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }

          .source-share {
            font-size: 12px;
            color: #999;
            display: flex;
            margin-top: 8px;
            align-items: center;

            .source-avatar {
              width: 22px;
              height: 22px;
              object-fit: cover;
              margin-right: 4px;
            }

            .nickname {
              flex: 1;
              width: 0;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              padding: 0 4px;
            }

            .share-time {
              color: #999;
              margin-left: auto;
              text-align: right;
              flex-shrink: 0;
            }
          }

          &.desc-o {
            .name {
              font-size: 12px;
              color: #202020;
              font-weight: 600;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
              .share-users {
                display: flex;
                color: #999;
                font-weight: normal;
                font-size: 12px;
                align-items: center;
                cursor: pointer;
                i {
                  position: relative;
                  top: 1px;
                  font-size: 13px;
                  &.ri-group-line {
                    margin-right: 4px;
                  }
                }
                img {
                  width: 13px;
                  font-size: 0;
                  margin-right: 2px;
                }
                .count {
                  font-size: 12px;
                  position: relative;
                  top: 1px;
                }
              }
            }
            .tip {
              font-size: 12px;
              color: #999;
              margin-top: 6px;
            }
          }
        }
      }
    }
  }

  .empty-mind-text {
    text-align: center;
    font-size: 15px;
    color: #999;
    margin-top: 72px;
  }
}
</style>
